package treebark

import (
	"errors"
	"gitee.com/damengde/treebark/mapper"
	"gitee.com/damengde/treebark/model"
	"gitee.com/damengde/treebark/services"
)

const (
	GormDb = iota + 1
)

// New 创建树服务
func New[T any](mapperType int, db any) (services.TreeService[T], error) {

	nodeField, err := model.New(new(T))
	if err != nil {
		return nil, err
	}
	dbMapper, err := newMapper[T](mapperType, db, nodeField)
	if err != nil {
		return nil, err
	}
	return services.NewDefaultService(dbMapper, nodeField), nil
}

func newMapper[T any](mapperType int, db any, nodeField *model.NodeField) (mapper.DBMapper[T], error) {
	switch mapperType {
	case GormDb:
		return mapper.NewGormDbMapper[T](db, nodeField)
	default:
		return nil, errors.New("not support mapper type")
	}
}
